package com.old.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

import org.apache.log4j.Logger;

import com.miracle.cache.Channel;
import com.miracle.db.DBPool;

public class DBUtil {
	private static final long serialVersionUID = 1L;

	public static boolean Mps_GetLock(Connection cdnNormalConn) throws SQLException {
		Statement stmt = cdnNormalConn.createStatement();
		int n = stmt.executeUpdate(LockSql);
		stmt.close();
		return n == 1;
	}


	public static void CheckExecBatch(Statement stmt, String msg) {
		for (int i = 0; i < 10; i++) {
			try {
				int[] nn = stmt.executeBatch();
				int countAll = 0;
				int countFail = 0;
				for (int n : nn) {
					countAll++;
					if (n != 1) {
						countFail++;
					}
				}
				if (countFail > 0 && countAll > 0) {
					StringBuilder returnResults = new StringBuilder();
					returnResults.append("exec ").append(msg).append("\n(").append(countFail).append('/').append(countAll).append(" fail/all sqls), returns=");

					for (int n : nn) {
						returnResults.append(n + ",");
					}
					if (ConfigInDB.doLog == 1) {
						log.warn(returnResults.toString());
					}
				}
				break;
			} catch (SQLException e) {
				MyUtils.printStackTrace(log, e);
			}
		}
	}

	public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		close(conn);
		close(pstmt);
		close(rs);
	}

	public static void close(Connection conn) {

		if (conn != null) {
			try {
				conn.close();
			} catch (Exception ex) {
				log.error("CloseConn error", ex);
			}
		}
	}

	public static void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (Exception ex) {
				log.error("CloseStatement error", ex);
			}
		}
	}

	public static void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (Exception ex) {
				log.error("CloseResultSet error", ex);
			}
		}
	}

	public static void Init() {
		log = Logger.getLogger(DBUtil.class);

		cdn_pool = new ConnectionPool("cdnconn", CdnDBConnString, ConfigInDB.CdnDBPoolSize, false);
		//		cdn_pool2 = new ConnectionPool("cdnconn2", CdnDBConnString2, ConfigInDB.CdnDBPoolSize, false);
		//		cdn_pool3 = new ConnectionPool("cdnconn3", CdnDBConnString3, ConfigInDB.CdnDBPoolSize, false);
		//
		//		syncJob_pool = new ConnectionPool("cdnJobconn", SyncJobConnString, ConfigInDB.CdnJobPoolSize, false);
		//		syncJob_pool2 = new ConnectionPool("cdnJobconn2", SyncJobConnString2, ConfigInDB.CdnJobPoolSize, false);
		//
		//		editor_pool = new ConnectionPool("editorconn", EditorDBConnString, ConfigInDB.EditorDBPoolSize, true);
		//		toodou_pool = new ConnectionPool("toodouconn", ToodouDBConnString, ConfigInDB.ToodouDBPoolSize, true);
		//		addb_poll = new ConnectionPool("addbconn", AdDBConnString, ConfigInDB.AdDBPoolSize, true);
		//
		//		hostname = java.net.InetAddress.getLocalHost().getHostName();
	}

	private static transient Logger log;
	private static String LockSql;

	public static String hostname;
	public static ConnectionPool cdn_pool, cdn_pool2, cdn_pool3, syncJob_pool, syncJob_pool2, editor_pool, toodou_pool, addb_poll;
	public static Random random = new Random();

	public static int JobTablePartitions = 3;

	//VVCDN
	public static String CdnDBConnString = "jdbc:mysql://10.5.111.4:3306/tdlive?user=root&password=root@tudou";

	public static void main(String[] args) {
		int n = 0;
		for (int i = 0; i < 1100; i++) {
			if (i == 0) {
				try {
					Class.forName("com.mysql.jdbc.Driver").newInstance();
				} catch (InstantiationException e1) {
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					e1.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
				Init();
			}
			long start = System.currentTimeMillis();
			Connection conn = null;
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			try {
				conn = DBUtil.cdn_pool.GetConnection();
				pstmt = conn.prepareStatement("select * from channel limit 1");
				rs = pstmt.executeQuery();
				Channel ch = new Channel();
				while (rs.next()) {
					ch.setId(rs.getInt(1));
					ch.setChannelName(rs.getString(2));
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (DBConnException e) {
				e.printStackTrace();
			} finally {
				DBPool.close(pstmt, rs, null);
				DBUtil.cdn_pool.BackConnection(conn);
			}
			long exec = (System.currentTimeMillis() - start);

			if (exec > 1) {
				n++;
				//				System.out.println(exec + "ms");
			}

		}
		System.out.println("dbUtil:" + n);
	}
}